﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Net.Mail;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class AdminSendMail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            OnlineUser ou = (OnlineUser)Session["User"];

            if (ou.UserAccessLevel != ("5"))
            {
                Response.Redirect("~/Default.aspx");
            }
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.AdminSendMail_PageTitle;

        }



        public void Button1_Click(object sender, EventArgs e)
        {

            lblMessage.Text = "";
            string Subject = ContactSubjectTextBox.Text.Trim();
            string Body = Environment.NewLine + ContentTextBox.Text.Trim();
            string MailSender = "admin@nivdal.com";
            //string MailSender = "nivdal@gmail.com";
            string MailReceiver = EmailTextBox.Text.Trim();

            if (ContentTextBox.Text.Length < 1) //Check if there are 1 chars of charcter minimum
                lblMessage.Text = Resources.Resource.Contact_EmptyTextBox;

            else

                try
                {
                    MailAddress SendFrom = new MailAddress(MailSender);
                    MailAddress SendTo = new MailAddress(MailReceiver);
                    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(SendFrom, SendTo);
                    message.Subject = Subject;
                    message.Body = " ";
                    message.Body+= Environment.NewLine + ContentTextBox.Text.Trim();
                    message.BodyEncoding = System.Text.Encoding.UTF8;
                    //SmtpClient emailClient = new SmtpClient("127.0.0.1");
                    SmtpClient emailClient = new SmtpClient();
                    // gmail only - remove when local
                    //emailClient.EnableSsl = true;
                    emailClient.Send(message);
                    this.lblMessage.Text = "mail was sent";
                    EmailTextBox.Text = "";
                    ContentTextBox.Text = "";
                    ContentTextBox.Text = "";
                }
                catch (Exception exc)
                {
                    lblMessage.Text = ("Send failure: " + exc.ToString());
                }

                finally { }
        }
    }   

}